﻿namespace InHD.utilities
{
    using System;
    using System.IO;
    using System.Text;
    using System.Diagnostics;

    /// <value>Available message severities</value>   
    public enum MessageType
    {
        /// <value>Informational message</value>     
        Informational = 1,
        /// <value>Failure audit message</value>     
        Failure = 2,
        /// <value>Warning message</value>     
        Warning = 3,
        /// <value>Error message</value>     
        Error = 4
    }

    /// <remarks
    /// Log messages to a file location.  
    /// </remarks>  
    public static class Logger
    {
        // Internal log file name value   
        private static string _FileName = "inhd_log.txt";

        // Internal log file location value   
        private static string _FileLocation = Directory.GetCurrentDirectory() + "\\";

        /// <summary>Log an exception.</summary>   
        /// <param name="Exce">Exception to log. </param>   
        /// <param name="Severity">Error severity level. </param>   
        public static void RecordMessage(Exception Exce, MessageType Severity)
        {
            RecordMessage("Message : " + Exce.Message + "Source : " + Exce.Source + " Trace: " + Exce.StackTrace, Severity);
        }

        /// <summary>Log a message.</summary>   
        /// <param name="Message">Message to log. </param>   
        /// <param name="Severity">Error severity level. </param>   
        public static void RecordMessage(string Message, MessageType Severity)
        {
            FileStream fileStream = null; 
            StreamWriter writer = null; 
            StringBuilder message = new StringBuilder(); 
            try
            {
                fileStream = new FileStream(_FileLocation + _FileName, FileMode.OpenOrCreate, FileAccess.Write); 
                writer = new StreamWriter(fileStream);
                
                // Set the file pointer to the end of the file      
                writer.BaseStream.Seek(0, SeekOrigin.End);
                
                // Create the message      
                message.Append(System.DateTime.Now.ToString()).Append(",").Append(Message);
                
                // Force the write to the underlying file      
                writer.WriteLine(message.ToString()); 
                writer.Flush();
            }
            finally
            {
                if (writer != null) writer.Close();
            }
        }
    }
}
